package cn.tedu.flower.manage.content.dao.persist.repository.impl;

import cn.tedu.flower.manage.common.pojo.vo.PageData;
import cn.tedu.flower.manage.common.util.PageInfoToPageDataConverter;
import cn.tedu.flower.manage.content.dao.persist.mapper.AftermarketMapper;
import cn.tedu.flower.manage.content.dao.persist.repository.IAftermarketRepository;
import cn.tedu.flower.manage.content.pojo.entity.Aftermarket;
import cn.tedu.flower.manage.content.pojo.vo.AftermarketListItemVO;
import cn.tedu.flower.manage.content.pojo.vo.AftermarketStandardVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.List;

/**
 * 处理售后数据的存储库实现类
 *
 * @author java@tedu.cn
 * @version 1.0
 */
@Slf4j
@Repository
public class AftermarketRepositoryImpl implements IAftermarketRepository {

    @Autowired
    private AftermarketMapper aftermarketMapper;

    public AftermarketRepositoryImpl() {
       log.info("创建存储库对象：AftermarketRepositoryImpl");
    }

    @Override
    public int insert(Aftermarket aftermarket) {
        log.debug("开始执行【插入售后】的数据访问，参数：{}", aftermarket);
        return aftermarketMapper.insert(aftermarket);
    }

    @Override
    public int insertBatch(List<Aftermarket> aftermarketList) {
        log.debug("开始执行【批量插入售后】的数据访问，参数：{}", aftermarketList);
        return aftermarketMapper.insertBatch(aftermarketList);
    }

    @Override
    public int deleteById(Long id) {
        log.debug("开始执行【根据ID删除售后】的数据访问，参数：{}", id);
        return aftermarketMapper.deleteById(id);
    }

    @Override
    public int deleteByIds(Collection<Long> idList) {
        log.debug("开始执行【批量删除售后】的数据访问，参数：{}", idList);
        return aftermarketMapper.deleteBatchIds(idList);
    }

    @Override
    public int update(Aftermarket aftermarket) {
        log.debug("开始执行【更新售后】的数据访问，参数：{}", aftermarket);
        return aftermarketMapper.updateById(aftermarket);
    }

    @Override
    public int count() {
        log.debug("开始执行【统计售后的数量】的数据访问，参数：无");
        return aftermarketMapper.selectCount(null);
    }

    @Override
    public int countByName(String nickname) {
        log.debug("开始执行【统计匹配名称的售后的数量】的数据访问，参数：{}", nickname);
        QueryWrapper<Aftermarket> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("nickname",nickname);

        return aftermarketMapper.selectCount(queryWrapper);
    }

    @Override
    public AftermarketStandardVO getStandardById(Long id) {
        log.debug("开始执行【根据ID查询售后信息】的数据访问，参数：{}", id);
        return aftermarketMapper.getStandardById(id);
    }

    @Override
    public List<AftermarketListItemVO> list() {
        log.debug("开始执行【查询售后列表】的数据访问，参数：无");
        return aftermarketMapper.list();
    }

    @Override
    public PageData<AftermarketListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始执行【查询售后列表】的数据访问，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<AftermarketListItemVO> list = aftermarketMapper.list();
        PageInfo<AftermarketListItemVO> pageInfo = new PageInfo<>(list);
        PageData<AftermarketListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }
}
